package org.fuys.coder.domain.act.lottery.service.algorithm.impl;

import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyRateMap;
import org.fuys.coder.domain.act.lottery.service.algorithm.BaseOddAlgorithm;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.security.SecureRandom;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.lottery.service.algorithm.impl
 * @className: AliasAlgorithm
 * @author: WangJing
 * @description: 需要高效处理抽奖请求的算法 别名算法 但是需要O(n^2)的初始化复杂度 本身是O(1)
 * @date: 2024/7/7 23:49
 * @version: 1.0
 */
@Service
public class AliasAlgorithm implements BaseOddAlgorithm {
    @Override
    public Integer randomDraw(Pair<Integer, Integer> type) {
        final List<Integer> mapValue = LotteryStrategyRateMap.getMapValue(type);
        if(ObjectUtils.isEmpty(mapValue)){
            return null;
        }
        final int randomVal = new SecureRandom().nextInt(100)+1;
        int idx=LotteryStrategyRateMap.hashIdx(randomVal);
        return mapValue.get(idx);
    }
}
